Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
unique_ptr.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Roc authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_core/unique_ptr.h
10//! @brief Unique ownrship pointer.
11
12#ifndef ROC_CORE_UNIQUE_PTR_H_
13#define ROC_CORE_UNIQUE_PTR_H_
14
15#include "roc_core/iallocator.h"
17#include "roc_core/panic.h"
18#include "roc_core/stddefs.h"
19
20namespace roc {
21namespace core {
22
23//! Unique ownrship pointer.
24//!
25//! @tparam T defines pointee type. It may be const.
26//! @tparam Destroyer is used to destroy the object.
27template <class T, class Destroyer = IAllocator> class UniquePtr : public NonCopyable<> {
28public:
29 //! Initialize null pointer.
31 : ptr_(NULL)
32 , destroyer_() {
33 }
34
35 //! Initialize from a raw pointer.
36 UniquePtr(T* ptr, Destroyer& destroyer)
37 : ptr_(ptr)
38 , destroyer_(&destroyer) {
39 }
40
41 //! Destroy object.
43 destroy_();
44 }
45
46 //! Reset pointer to null.
47 void reset() {
48 destroy_();
49 ptr_ = NULL;
50 destroyer_ = NULL;
51 }
52
53 //! Reset pointer to a new value.
54 void reset(T* new_ptr, Destroyer& new_destroyer) {
55 if (new_ptr != ptr_) {
56 destroy_();
57 ptr_ = new_ptr;
58 destroyer_ = &new_destroyer;
59 }
60 }
61
62 //! Get underlying pointer and pass ownership to the caller.
63 T* release() {
64 T* ret = ptr_;
65 if (ret == NULL) {
66 roc_panic("uniqueptr: attempting to release a null pointer");
67 }
68 ptr_ = NULL;
69 destroyer_ = NULL;
70 return ret;
71 }
72
73 //! Get underlying pointer.
74 T* get() const {
75 return ptr_;
76 }
77
78 //! Get underlying pointer.
79 T* operator->() const {
80 return ptr_;
81 }
82
83 //! Get underlying reference.
84 T& operator*() const {
85 if (ptr_ == NULL) {
86 roc_panic("unique ptr: attempting to dereference a null pointer");
87 }
88 return *ptr_;
89 }
90
91 //! Convert to bool.
92 operator const struct unspecified_bool*() const {
93 return (unspecified_bool*)ptr_;
94 }
95
96private:
97 void destroy_() {
98 if (ptr_) {
99 roc_panic_if(destroyer_ == NULL);
100 destroyer_->destroy(*ptr_);
101 }
102 }
103
104 T* ptr_;
105 Destroyer* destroyer_;
106};
107
108} // namespace core
109} // namespace roc
110
111#endif // ROC_CORE_UNIQUE_PTR_H_
Base class for non-copyable objects.
Definition: noncopyable.h:23
Unique ownrship pointer.
Definition: unique_ptr.h:27
void reset(T *new_ptr, Destroyer &new_destroyer)
Reset pointer to a new value.
Definition: unique_ptr.h:54
UniquePtr(T *ptr, Destroyer &destroyer)
Initialize from a raw pointer.
Definition: unique_ptr.h:36
T & operator*() const
Get underlying reference.
Definition: unique_ptr.h:84
T * operator->() const
Get underlying pointer.
Definition: unique_ptr.h:79
void reset()
Reset pointer to null.
Definition: unique_ptr.h:47
~UniquePtr()
Destroy object.
Definition: unique_ptr.h:42
T * get() const
Get underlying pointer.
Definition: unique_ptr.h:74
UniquePtr()
Initialize null pointer.
Definition: unique_ptr.h:30
T * release()
Get underlying pointer and pass ownership to the caller.
Definition: unique_ptr.h:63
Memory allocator interface.
Root namespace.
Non-copyable object.
Panic function.
#define roc_panic_if(x)
Panic if condition is true.
Definition: panic.h:26
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition: panic.h:42
Commonly used types and functions.